package com.example.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.vo.Result;
import com.example.sys.entity.User;
import com.example.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuage
 * @since 2023-12-20
 */
//@CrossOrigin
@Api(tags = {"用户接口列表"})
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("/all")
    public Result<List<User>> getAllUser(){

        return Result.success(userService.list(),"查询成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody User user){
        Map<String,Object> data = userService.login(user);
        if (data != null) {
            return Result.success(data);

        }
        return  Result.fail(20002,"用户名或密码错误");
    }


    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestHeader("X-Token") String token){
//        根据token获取用户信息 redis
        Map<String,Object> data= null;
        try {
            data = userService.getUserInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (data != null) {
            return Result.success(data);
        }
        return  Result.fail(20003,"登录信息无效请重新登录");
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<Map<String,Object>> getUserList(@RequestParam(value = "username",required = false) String username,
                                              @RequestParam(value = "phone",required = false)String phone,
                                              @RequestParam(value = "pageNo")Long pageNo,
                                              @RequestParam(value = "pageSize")Long pageSize){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(username),"username",username);
        wrapper.like(StringUtils.hasLength(phone),"phone",phone);
        Page<User> page = new Page<>(pageNo,pageSize);
        userService.page(page,wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        return Result.success(data);
    }

    @PostMapping("/adduser")
    public Result<?> addUser(@RequestBody User user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.addUser(user);
        return Result.success("新增用户成功");
    }

    @PutMapping("/updateuser")
    public Result<?>  updateUser(@RequestBody User user){
        user.setPassword(null);
        userService.updateUser(user);
        return Result.success("修改用户成功");
    }

    @GetMapping("/getuser/{id}")
    public Result<User> getUserByID(@PathVariable Integer id){
        User user = userService.getUserByID(id);
        return Result.success(user);
    }

    @DeleteMapping("/deleteuser/{id}")
    public Result<User> deletUserByID(@PathVariable Integer id){
        userService.deletUserByID(id);
        return Result.success("删除用户成功");
    }
}
